package cn.wizzer.app.web.modules.controllers.user;


import cn.wizzer.app.user.modules.models.GroupInfo;
import cn.wizzer.app.user.modules.models.GroupMember;
import cn.wizzer.app.user.modules.models.constant.ChatConstant;
import cn.wizzer.app.user.modules.services.GroupApplyService;
import cn.wizzer.app.user.modules.services.GroupInfoService;
import cn.wizzer.app.user.modules.services.GroupMemberService;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.filter.ApiTokenFilter;
import cn.wizzer.framework.base.model.enums.BizExceptionEnum;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.util.param.ParamVerify;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;

/**
 * 可申请群列表、用户申请入群
 *
 * @author .
 */
@IocBean
@At("/api/user/group")
public class ApiGroupController {
    @Reference
    @Inject
    private GroupInfoService groupInfoService;
    @Reference
    @Inject
    private GroupMemberService groupMemberService;
    @Reference
    @Inject
    private GroupApplyService groupApplyService;
    @Reference
    @Inject
    private UserService userService;


    /**
     * 用户所在群列表
     */
    @At("/list")
    @Ok("json:full")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object list(@ParamVerify(required = true, name = "pageNumber") int pageNumber,
                       @ParamVerify(required = true, name = "pageSize") int pageSize) {
        String appid = Mvcs.getReq().getHeader("appid");
        return Result.success().addData(groupInfoService.list(appid, pageNumber, pageSize));
    }


    /**
     * 可申请群列表(不在群中、公开)
     */
    @At("/applyList")
    @Ok("json:full")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object applyList(@ParamVerify(required = true, name = "pageNumber") int pageNumber,
                            @ParamVerify(required = true, name = "pageSize") int pageSize) {
        String appid = Mvcs.getReq().getHeader("appid");
        return Result.success().addData(groupInfoService.applyList(appid, pageNumber, pageSize));
    }


    /**
     * 群成员列表
     */
    @At("/memberList")
    @Ok("json:full")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object memberList(@ParamVerify(required = true, name = "groupId") String groupId,
                             @ParamVerify(required = true, name = "pageNumber") int pageNumber,
                             @ParamVerify(required = true, name = "pageSize") int pageSize) {
        String appid = Mvcs.getReq().getHeader("appid");
        return Result.success().addData(groupMemberService.list(appid, groupId, pageNumber, pageSize));
    }


    /**
     * 申请入群
     *
     * @return
     */
    @At("/apply")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object apply(@ParamVerify(name = "groupId", required = true) String groupId,
                        @ParamVerify(name = "content") String content) {
        if (Strings.isBlank(groupId)) {
            return Result.error(BizExceptionEnum.NOT_CHOOSE_GROUP);
        }
        String appid = Mvcs.getReq().getHeader("appid");
        // 判断是否已群中
        GroupMember groupMember = groupMemberService.fetch(Cnd.where("userId", "=", appid).and("groupId", "=", groupId));
        if (null != groupMember) {
            return Result.error(BizExceptionEnum.ALREADY_IN_THE_GROUP);
        }
        groupApplyService.apply(appid, groupId, content);
        return Result.success();
    }


    /**
     * 管理员查找用户列表
     *
     * @return
     */
    @At("/userList")
    @Ok("json:full")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object userList(@ParamVerify(name = "keyWord") String keyWord,
                           @ParamVerify(required = true) int pageNumber,
                           @ParamVerify(required = true) int pageSize) {
        return Result.success(userService.userList(keyWord, pageNumber, pageSize));
    }


    /**
     * 创建私聊
     *
     * @return
     */
    @At("/privateChat")
    @Ok("json:full")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object privateChat(@ParamVerify(required = true, name = "userId") String userId) {
        String appid = Mvcs.getReq().getHeader("appid");
        // 双方如果已经在一个私聊群中,不再创建
        GroupInfo groupInfo = groupInfoService.onPrivateChat(appid, userId);
        if (null != groupInfo) {
            return Result.success().addData(groupInfo);
        }
        return Result.success().addData(groupInfoService.privateChat(appid, userId, ChatConstant.PRI_CHAT));
    }


}
